package com.raincat.tools.multimediapickertools.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;

import com.airbnb.lottie.LottieAnimationView;
import com.bumptech.glide.Glide;
import com.raincat.tools.R;

import java.util.List;

/**
 * <pre>
 *     author : RainCat
 *     e-mail : nining377@gmail.com
 *     time   : 2022/03/01
 *     desc   : 一个提示弹窗
 *     version: 1.0
 * </pre>
 */

public class TipsPopWindow extends PopupWindow {
    private Context context;

    public TipsPopWindow(Context context) {
        super(context);
        this.context = context;
    }

    public TipsPopWindow(final Context context, View parent) {
        super(context);
        this.context = context;

        View view = View.inflate(this.context, R.layout.picker_view_tips, null);
        setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
        setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        setBackgroundDrawable(null);
        setFocusable(true);
        setOutsideTouchable(false);
        setContentView(view);
        showAtLocation(parent, Gravity.NO_GRAVITY, 0, 0);
        update();

        LottieAnimationView lav_tips = view.findViewById(R.id.lav_tips);
        TextView tv_tips = view.findViewById(R.id.tv_tips);

        lav_tips.setImageAssetsFolder("choose");
        lav_tips.setAnimation("choose.json");
        //是否循环执行
        lav_tips.setRepeatCount(-1);
        //执行动画
        lav_tips.playAnimation();

        tv_tips.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
                preferences.edit().putBoolean("tips", true).apply();
                dismiss();
            }
        });
    }

    /**
     * 防止安卓8以上弹出框位置错误
     */
    @Override
    public void showAsDropDown(View anchor) {
        if (Build.VERSION.SDK_INT >= 24) {
            Rect rect = new Rect();
            anchor.getGlobalVisibleRect(rect);
            int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
            setHeight(h);
        }
        super.showAsDropDown(anchor);
    }
}
